<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms 
of the GNU General Public License as published by the Free Software Foundation; either 
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; 
if not, write to the 
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2007
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
*	BIBTEXPAGEPARSE: BibTeX PAGES import class
*****/
class BIBTEXPAGEPARSE
{
// Constructor
	function BIBTEXPAGEPARSE()
	{
		$this->completeField = FALSE; // take first valid number in some cases
	}
//calls the page parser and returns an array with first- and lastpage
	function init($item)
	{
		$item = trim($item);
		if(!$item)
			return array(FALSE, FALSE);
		else if ($this->completeField) //if true, return the complete item, else return only the first number found.
			return array($item, FALSE);
		if ($this->parsePages($item))
			return $this->return; // first and last page present.
// return whatever we have
		return array($item, FALSE);
	}

	//parsePages tries to split on '--' or '-' (in case no valid split on '--' is possible.
	//if the split results in 2 elements, the split is considered valid.
	function parsePages($pages)
	{
		$start = $end = FALSE;
		$elements = preg_split("/--/", $pages);
		//first split on the valid bibtex page separator
		if (count($elements) == 1) 
		{
			//no '--' found, try on single '-'
			$elements = preg_split("/-/", $pages);
		}
		//try on ','
		if (count($elements) == 1) 
		{
			$elements = preg_split("/,/", $pages);
		}
		if (count($elements) == 2)
		{	
			//found valid pages that are separated by '--' or by '-'
			$start = trim($elements[0]);
			$end = trim($elements[1]);
// if [1] < [0], this might be e.g. 456-76 or 456,76 inferring 456-476.  Will only work if arabic numerals
			if(is_numeric($start) && is_numeric($end) && ($end < $start))
				$end = substr($start, 0, strlen($start) - strlen($end)) . $end;
			$this->return = array($start, $end);
			return TRUE;
		}
		return FALSE;
	}
}
?>